#include "sudoku_algorithm.h"
//------------------------------------------------------------------------------------------------------------
void SudokuSolvingRecursive::solve(SudokuBoard* pSudBoard, vector<int>::iterator fieldIdxIt, vector<int>::iterator* endField)
{
    this->endField = endField;
    sudBoard = pSudBoard;
    solve(fieldIdxIt);
}
//------------------------------------------------------------------------------------------------------------
void SudokuSolvingRecursive::solve(vector<int>::iterator fieldIdxIt)
{
    map<int,SudokuField*>::iterator fieldIt;
    SudokuField* f;

    f = (*(sudBoard->getBoard()))[*fieldIdxIt];

    if(fieldIdxIt == *endField)// fieldIt->second->getAvailableValues()->end())
    {
        cout << "this is last field on board.\n";
        sudBoard->snapshot(); //dodaj rozwiązanie do zbioru rozwiązań
        return;
    }

    if(f == NULL)
    {
    	cout << "ERROR_NULL!!\n";
    	//sudBoard->print();
    	return;
    }

	sudBoard->updateAvailableSet(f);

    for(f->value = f->getAvailableValues()->begin();
        (f->value != f->getAvailableValues()->end()) &&
            (sudBoard->getSolutions()->size() < sudBoard->getMaxsol());
        f->value++)
    {
//        f->print();
    	sudBoard->laps++;

//        cout << "solving iteration for f[" << f->getId() << "]=" << *(f->value) << endl;
        if(*(f->value) != 0)
        {
            //cout << "call solve() recursive\n";
            solve(boost::next(fieldIdxIt));
        }
        else
        {
            //if(f->getAvailableValues()->size() == 1)
                //cout << "Set of available values for f["<<f->getId()<<"] is EMPTY!!\n";
        }

    }

    f->value = f->getAvailableValues()->begin();
//	cout << "this: "<<(int)this<<"laps="<<getLaps()<<endl;
    //cout << "before exit: v[" << f->getId() << "]="<< *(f->value) << endl;
	//cout << "[EXIT]\tSudokuSolvingRecursive::solve\n";
}
//------------------------------------------------------------------------------------------------------------
